var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
//修改id缓存，传递分组参数
function checkGroup(checkId,el) {
    console.log(el);
    sessionStorage.setItem("checkId",checkId);
    web.role_list();
}
function delete_button(buttonId) {
    console.log(buttonId)
    layer.confirm('确认要删除此按钮吗？',function () {
        $.ajax({
            type: "POST",
            url: root.accessInter.access_button_delete,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                buttonId:buttonId
            },  //传入组装的参数
            success: function (data) {
                if (data.errorCode == 0) {
                    layer.msg("删除成功",{icon: 1,time:1000},function () {
                        window.location.reload();
                    });
                } else {
                    layer.msg(data.errorMessage, {icon: 5, time: 1000});
                }
            },
        });
    })
}
web.role_list=(function(){
    return function(page,current,obj,scrollLoad){
        $.ajax({
            contentType: 'application/json',
            url: root.accessInter.access_button_query,
            cache: false, //禁用缓存
            type: "POST",
            data: JSON.stringify({
                pageSize: 50,
                nowPage: 1
            }),
            success: function(data) {
                if (Number(data.errorCode) == 0){
                    console.log(data)

                    $(".removeEl").remove();//清空分组
                    dataSource.splice(0,dataSource.length);//清空数组

                    $.each(data.list.list, function (i, field) {
                        var rowData = new Array();
                        rowData.push(field.button_id);
                        rowData.push(field.button_name);
                        rowData.push(field.status);
                        rowData.push(field.create_time);
                        dataSource.push(rowData);
                    });

                    initDataTable(dataSource);
                    function initDataTable(dataSource) {
                        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
                        dttable.fnClearTable(); //清空一下table
                        dttable.fnDestroy(); //还原初始化了的datatable
                        $("#datatable").DataTable({
                            "autoWidth": false,
                            "ordering": true,
                            "searching": true,
                            "paging": true,
                            "retrieve": true,
                            "destroy": true,
                            "bAutoWidth": true,
                            "bLengthChange": true,
                            "bSort": true,
                            "aaSorting": [[3,'desc'],],//数据排序
                            "aLengthMenu":[10,20,30],
                            "oLanguage": {
                                "sLengthMenu": "每页显示 _MENU_ 条记录",
                                "sZeroRecords": "抱歉， 没有找到",
                                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                                "sInfoEmpty": "没有数据",
                                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                                "oPaginate": {
                                    "sFirst": "首页",
                                    "sPrevious": "前一页",
                                    "sNext": "后一页",
                                    "sLast": "尾页"
                                },
                                "sZeroRecords": "没有检索到数据",
                                "sProcessing": "<img src='./loading.gif' />"
                            },
                            data: dataSource,
                            columnDefs: [
                                {
                                    "visible": true,
                                    "targets": 0
                                },
                                {
                                    "render": function(data,type,row,meta){
                                        /*var val = '<div style="width: 60px;height:20px;overflow: hidden;margin: auto;" title="'+row[0]+'">'+row[0]+'</div>';*/
                                        var val = '<div>'+row[0]+'</div>';
                                        return val;
                                    },
                                    "targets": 0
                                },
                                {
                                    "render": function(data,type,row,meta){
                                        var val = '<div">'+row[1]+'</div>';
                                        return val;
                                    },
                                    "targets": 1
                                },
                                {
                                    "render": function(data,type,row,meta){
                                        var val = '<div>'+row[3]+'</div>';
                                        return val
                                    },
                                    "targets": 2
                                },
                                {
                                    "render": function(data,type,row,meta){
                                        if(Number(row[2])==0){
                                            var val = '<span class="dropDown"> <a class="dropDown_A" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">开启<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li onclick="button_stop(\'\',\''+ row +'\')"><a href="#">禁用</a></li></ul></span>';
                                            return val;
                                        }
                                        if(Number(row[2])==1) {
                                            var val = '<span class="dropDown"> <a class="dropDown_A" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">禁用<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li onclick="button_start(\'\',\''+ row +'\')"><a href="#">开启</a></li></ul></span>';
                                            return val;
                                        }
                                    },
                                    "targets": 3
                                },
                                {
                                    "render": function(data,type,row,meta){
                                        var val = '<a><span class="list" onclick="button_edit(\'修改信息\',\'button_add.html\',\''+row+'\')">编辑</span>&nbsp;&nbsp;<span a href="##" class="list" onclick="delete_button(\''+ row[0]+'\')">删除</span></a>';
                                        return val;
                                    },
                                    "targets": 4
                                },
                            ]
                        });
                    };
                } else {
                    alert("服务异常")
                }
            }
        });
    }
})();
//角色组收起展开
var iClick2=0
$('.rolegroup_box .btn_open').click(function(){
    iClick2++;
    if(iClick2%2!=0){
        console.log($('.ROLE_GROUP'))
        $('.ROLE_GROUP').removeClass('hide');
        $(".btn_open span").html("收起");
    }else{
        $('.ROLE_GROUP').addClass('hide');
        //$("#more").attr("class", "hidden");
        //$("#less").attr("class", "lessAdd")
        $(".btn_open span").html("展开");
    }
})

function getCodeList(id) {
    var user_data = {
        ROLE_ID: id
    }
    var succCallBack = function (data, status, response) {
        var datacunko = []
        if(data.userList == undefined){
                var datacunko = '';
            }else {
                   for (var i = 0; i < data.userList.length; i++) {
                    datacunko += data.userList[i].NAME + '(' + data.userList[i].USERNAME + ')' + '  ';
            }
            $('.codeWrap').attr("title",  datacunko)
        }
    };
     web.doAjax(root.accessInter.access_role_listUser, 'post', user_data, succCallBack);
}
getCodeList();

function edit_roleId(title,url,roleId,roleName) {
    console.log()
    sessionStorage.setItem("groupId",roleId);
    sessionStorage.setItem("groupName",roleName);
    sessionStorage.setItem("groupEdit",true);

    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
function role_from(title,url,row){
    window.session_storage.setItem("roleId_to_roleForm",row);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

//=====================================================================================================================
/*停用*/
function button_stop(obj,id){
    var arr = id.split(",");
    layer.confirm('确认要停用吗？',function(index){
        console.log(id)
        $.ajax({
            type: "POST",
            url: root.accessInter.access_button_update,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                buttonId: arr[0],
                buttonName: arr[1],
                buttonStatus: "1"
            },  //传入组装的参数
            success: function (data) {
                if (data.errorCode == 0) {
                    layer.msg("保存成功",{icon: 1,time:1000},function () {
                        window.location.reload();
                    });
                } else {
                    layer.msg(data.errorMessage, {icon: 5, time: 1000});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        });
    });
}

/*启用*/
function button_start(obj,id){
    var arr = id.split(",");
    layer.confirm('确认要启用吗？',function(index){
        $.ajax({
            type: "POST",
            url: root.accessInter.access_button_update,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                buttonId: arr[0],
                buttonName: arr[1],
                buttonStatus: "0"
            },  //传入组装的参数
            success: function (data) {
                if (data.errorCode == 0) {
                    layer.msg("保存成功",{icon: 1,time:1000},function () {
                        window.location.reload();
                    });
                } else {
                    layer.msg(data.errorMessage, {icon: 5, time: 1000});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        });
    });
}
